//
//  CYListViewController.m
//  Demo
//
//  Created by lc4y on 2017/11/5.
//  Copyright © 2017年 lc4y. All rights reserved.
//

#import "CYListViewController.h"

@interface CYListViewController ()

@property (nonatomic , strong, readwrite) UILabel *baseTitleLabel;

@end

@implementation CYListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self cy_configNavBar];
    [self cy_configSubView];
    [self cy_configListViewStyle];
    [self cy_masLayoutSubView];
    [self cy_loadData];
}

- (void)cy_configNavBar {}

- (void)cy_loadData {}

- (void)cy_configSubView {}

- (void)cy_configListViewStyle {}

- (void)base_configNavigationBar {
    //设置返回按钮
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
}

/** 设置列表的Title */
- (void)setCy_listTitle:(NSString *)cy_listTitle {
    
    if( !cy_listTitle ) {
        return;
    }
    
    _cy_listTitle = cy_listTitle;
    self.baseTitleLabel.text = _cy_listTitle;
    [self.view addSubview:self.baseTitleLabel];
}

- (void)cy_masLayoutSubView {
    if (!self.cy_listTitle) {
        return;
    }
    
    if([_baseTitleLabel superview] == self.view){
        self.baseTitleLabel.frame = CGRectMake(20, 64, ZHYScreenWidth-40, kBaseTitleLabelHeight);
    } else {
        self.baseTitleLabel.frame = CGRectMake(20, -kBaseTitleLabelHeight, ZHYScreenWidth-40, kBaseTitleLabelHeight);
    }
}

- (void)cy_createListView:(NSString *)listViewClass viewType:(CYListViewType)viewType {
    switch (viewType) {
        case CYListViewTableView:
            self.cy_listView = [[NSClassFromString(listViewClass) alloc] init];
            self.cy_listView.cy_eventDelegate = self;
            [self.view addSubview:self.cy_listView];
            
            if(self.cy_listTitle) {
                self.cy_listView.cy_tableView.contentInset = UIEdgeInsetsMake(kBaseTitleLabelHeight, 0, 0, 0);
                [self.cy_listView.cy_tableView setContentOffset:CGPointMake(0, -kBaseTitleLabelHeight)];
                [self.cy_listView.cy_tableView addSubview:self.baseTitleLabel];
            }
            break;
        case CYListViewCollectionView:
            self.cy_collectionView = [[NSClassFromString(listViewClass) alloc] init];
            self.cy_collectionView.cy_eventDelegate = self;
            [self.view addSubview:self.cy_collectionView];
            if(self.cy_listTitle) {
                self.cy_collectionView.cy_collectionView.contentInset = UIEdgeInsetsMake(kBaseTitleLabelHeight, 0, 0, 0);
                [self.cy_collectionView.cy_collectionView addSubview:self.baseTitleLabel];
                [self.cy_collectionView.cy_collectionView setContentOffset:CGPointMake(0, -kBaseTitleLabelHeight)];
            }
            break;
        default:
            break;
    }
}

- (void)cy_createListView:(NSString *)listViewClass listViewStyle:(UITableViewStyle)listStyle {
    self.cy_listView = [[NSClassFromString(listViewClass) alloc] initWithStyle:listStyle];
    self.cy_listView.cy_eventDelegate = self;
    [self.view addSubview:self.cy_listView];
    
    if(self.cy_listTitle) {
        self.cy_listView.cy_tableView.contentInset = UIEdgeInsetsMake(kBaseTitleLabelHeight, 0, 0, 0);
        [self.cy_listView.cy_tableView setContentOffset:CGPointMake(0, -kBaseTitleLabelHeight)];
        [self.cy_listView.cy_tableView addSubview:self.baseTitleLabel];
    }
}

- (void)cy_createCollectionView:(NSString *)listViewClass collectionViewLayout:(UICollectionViewFlowLayout *)layout {
    self.cy_collectionView = [[NSClassFromString(listViewClass) alloc] initWithLayout:layout];
    self.cy_collectionView.cy_eventDelegate = self;
    [self.view addSubview:self.cy_collectionView];
    if(self.cy_listTitle) {
        self.cy_collectionView.cy_collectionView.contentInset = UIEdgeInsetsMake(kBaseTitleLabelHeight, 0, 0, 0);
        [self.cy_collectionView.cy_collectionView addSubview:self.baseTitleLabel];
        [self.cy_collectionView.cy_collectionView setContentOffset:CGPointMake(0, -kBaseTitleLabelHeight)];
    }
}


- (void)cy_didSelectCellRow:(NSIndexPath *)rowIndexPath {}

- (void)cy_editCellRow:(NSIndexPath *)rowIndexPath menuIndex:(NSInteger)menuIndex {}

- (void)cy_listViewDidScroll:(UIScrollView *)scrollView {

    if ( scrollView.contentOffset.y >= 0 ) {
        [self setNavigationBarTitle:self.baseTitleLabel.text];
        self.baseTitleLabel.hidden = YES;
        //self.navigationBar.alpha = 1;
    } else {
        [self setNavigationBarTitle:@""];
        self.baseTitleLabel.hidden = NO;
        //self.navigationBar.alpha = 0;
    }
}

- (UILabel *)baseTitleLabel {
    if ( !_baseTitleLabel ) {
        _baseTitleLabel = [UILabel new];
        _baseTitleLabel.cyTextColor([UIColor whiteColor]);
        _baseTitleLabel.font = [UIFont boldSystemFontOfSize:40];
    }
    return _baseTitleLabel;
}

@end
